﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNS_BLL;
using QLNS_Public;

namespace QLNS_GiaoDien
{
    public partial class uc_THAISAN : UserControl
    {
        public uc_THAISAN()
        {
            InitializeComponent();
        }
        ThaiSan_BLL thaisan_bll = new ThaiSan_BLL();
        ThaiSan_Public ts = new ThaiSan_Public();

        private void uc_THAISAN_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = thaisan_bll.LayDuLieu();
        }
        int flag;
        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = 0;
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = 1;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            string s;
            ts.MaThaiSan = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Thai Sản"));
            s = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Họ Và Tên").ToString();
            if (MessageBox.Show("Bạn có muốn xóa Mã Thai Sản: '" + ts.MaThaiSan + "' của Nhân viên: '" + s + "'", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                thaisan_bll.delete_ThaiSan(ts);
                uc_THAISAN_Load(sender, e);
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                ts.MaNhanVien = int.Parse(cob_MaNV.Text);
                ts.NgayVeSom = Convert.ToDateTime(dateTime_NgayVeSom.Text);
                ts.NgayNghiSinh = Convert.ToDateTime(dateTime_NgayNghiSinh.Text);
                ts.NgayLamTroLai = Convert.ToDateTime(dateTime_NgayDiLamtrolai.Text);
                ts.TroCapCongTy=float.Parse(txt_Trocap.Text);
                ts.GhiChu=txt_ghichu.Text;
                thaisan_bll.Isert_ThaiSan(ts,ts,ts,ts,ts,ts);
                MessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (flag == 1)
            {
                ts.MaThaiSan = int.Parse(txt_MaThansan.Text);
                ts.MaNhanVien = int.Parse(cob_MaNV.Text);
                ts.NgayVeSom = Convert.ToDateTime(dateTime_NgayVeSom.Text);
                ts.NgayNghiSinh = Convert.ToDateTime(dateTime_NgayNghiSinh.Text);
                ts.NgayLamTroLai = Convert.ToDateTime(dateTime_NgayDiLamtrolai.Text);
                ts.TroCapCongTy = float.Parse(txt_Trocap.Text);
                ts.GhiChu = txt_ghichu.Text;
                thaisan_bll.Update_ThaiSan(ts,ts, ts, ts, ts, ts, ts);
                MessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            uc_THAISAN_Load(sender,e);
        }

        private void cob_MaNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaThansan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void cob_MaNV_TextChanged(object sender, EventArgs e)
        {
            string sql = "select tbl_NhanVien.HoTen,tbl_PhongBan.TenPhong from tbl_NhanVien,tbl_PhongBan where tbl_NhanVien.MaPhong=tbl_PhongBan.MaPhong and tbl_NhanVien.MaNhanVien='"+cob_MaNV.Text+"'";
            DataTable dt = new DataTable();
            dt = thaisan_bll.taoBang(sql);
            cob_TenNV.DataSource = dt;
            cob_TenNV.DisplayMember = "HoTen";
            cob_PhongBan.DataSource = dt;
            cob_PhongBan.DisplayMember = "TenPhong";
        }

    }
}
